package nexus.com.grid
{
	import com.ux.util.TextFieldUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import nexus.base.NXSprite;
	import nexus.com.textfield.TruncateTextField;
	import nexus.definition.Align;
	
	public class GridItem extends NXSprite
	{
		//-----------------------------------------------------------------------------------
		//
		// private vars
		//
		//-----------------------------------------------------------------------------------
		
		private var _data : Object;
		
		private var _overBg : Shape;
		private var _overBgColor : uint = 0xe4f9ff;
		
		private var _list : Array = [];
		private var _columns : Array = [];
		private var _columnsWids : Array = [];
		
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		//
		// public vars
		//
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function GridItem()
		{
			super();
			
			backGroundColor = 0xffffff;
			backGroundAlpha = 0;
			
//			viewWidth = 200;
			viewHeight = 39;
			
			_overBg = new Shape();
			var g : Graphics = _overBg.graphics;
			g.beginFill( _overBgColor, 1 );
			g.drawRect( 0,0, viewWidth,viewHeight );
			g.endFill();
			_overBg.alpha = 0;
			addChild( _overBg );
		}
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------

		public function get columnsWids():Array
		{
			return _columnsWids;
		}

		public function set columnsWids(value:Array):void
		{
			_columnsWids = value;
		}

		public function get list():Array
		{
			return _list;
		}
		
		public function set list(value:Array):void
		{
			_list = value;
		}
		
		public function get columns():Array
		{
			return _columns;
		}
		
		public function set columns(value:Array):void
		{
			_columns = value;
		}

		override public function set viewWidth(value:Number):void
		{
			super.viewWidth = value;
			if(_overBg != null) _overBg.width = viewWidth;
		}

		override public function set viewHeight(value:Number):void
		{
			super.viewHeight = value;
			if(_overBg != null) _overBg.height = viewHeight;
		}

		public function get overBgColor():uint
		{
			return _overBgColor;
		}

		public function set overBgColor(value:uint):void
		{
			_overBgColor = value;
			
			var g : Graphics = _overBg.graphics;
			g.clear();
			g.beginFill( _overBgColor, 1 );
			g.drawRect( 0,0, viewWidth,viewHeight );
			g.endFill();
		}


		public function set data( __data : Object ):void
		{
			this._data = __data;
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		//-----------------------------------------------------------------------------------
		//
		// public function
		//
		//-----------------------------------------------------------------------------------
		
		override public function updateDisplayList():void
		{
			super.updateDisplayList();
			
			_overBg.width = viewWidth;
			_overBg.height = viewHeight;
			
			
			// 아이템 셋팅을 먼저 하고,
			setGridData();
			
			
			// 정렬하기.
			var sum : Number = 0;
			var listSum : Number = 0;
			var hAlign : String = "";
			var vAlign : String = "";
			var cellInitX : Number = 0;
			var cellWid : Number = 0;
			var cellHei : Number = 0;
			var cellListGap : Number = 5;
			var len:int = _list.length;
			
			for (var i:int = 0; i < len; i++) 
			{
				if( _list[ i ] != null )
				{
					hAlign = ( _columns[ i ] as GridColumn ).alignHrizontal;
					vAlign = ( _columns[ i ] as GridColumn ).alignVertical;
					cellInitX = sum; // 셀의 시작점.
					cellWid = _columnsWids[ i ]; // 셀의 너비
					cellHei = viewHeight; // 셀의 높이
					
					if( _list[ i ] is Array )
					{
						var jSum : Number = 0;
						
						for (var j:int = 0; j < _list[ i ].length; j++) 
						{
							// 가로 정렬.
							if( hAlign == Align.LEFT || Align.CENTER ) _list[ i ][ j ].x = int( cellInitX + jSum); // 1. 센터정렬이면 우선 좌측정렬로 해놓고,
							else if( hAlign == Align.RIGHT ) _list[ i ][ j ].x = int( cellInitX + cellWid - jSum - _list[ i ][ j ].width );
							
							jSum += _list[ i ][ j ].width + cellListGap;
							
							// 세로정렬
							if( vAlign == Align.TOP ) _list[ i ][ j ].y = 0;
							if( vAlign == Align.MIDDLE ) _list[ i ][ j ].y = int( ( cellHei-_list[ i ][ j ].height )/2 );
							if( vAlign == Align.BOTTOM ) _list[ i ][ j ].y = int( cellHei-_list[ i ][ j ].height );
						}
						
						// 2. 센터정렬이면 우측으로 적당량 밀어준다.
						if( hAlign == Align.CENTER )
						{
							var plusNum : Number = ( cellWid - jSum )/2;
							for each( var obj : DisplayObject in _list[ i ] )
							{
								obj.x += plusNum;
							}
						}
					}
					else
					{
						
						if( list[ i ] is TruncateTextField )
						{
							( list[ i ] as TruncateTextField ).widthToTruncate = cellWid;
							( list[ i ] as TruncateTextField ).apply();
						}
						
						// 가로 정렬.
						if( hAlign == Align.LEFT ) _list[ i ].x = int( cellInitX );
						else if( hAlign == Align.RIGHT ) _list[ i ].x = int( cellInitX + cellWid - _list[i].width );
						else _list[ i ].x = int( ( cellWid-_list[i].width )/2+cellInitX );
						
						// 세로정렬
						if( vAlign == Align.TOP ) _list[ i ].y = 0;
						if( vAlign == Align.MIDDLE ) _list[ i ].y = int( ( cellHei-_list[ i ].height )/2 );
						if( vAlign == Align.BOTTOM ) _list[ i ].y = int( cellHei-_list[ i ].height );
						
					}
				}
				
				sum += _columnsWids[ i ];
			}
		}
		
		
		
		public function ON():GridItem
		{
			_overBg.alpha = 1;
			return this;
		}
		
		
		public function OFF():GridItem
		{
			_overBg.alpha = 0; 
			return this;
		}
		
		
		public function reset():GridItem
		{
			removeAndDestoryChild();
			return this;
		}
		
		//-----------------------------------------------------------------------------------
		//
		// protected function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private function
		//
		//-----------------------------------------------------------------------------------
		
		
		// data 셋팅
		private function setGridData():void
		{
			if( _columns == null || _columns.length == 0 ) return; 
			
			removeAndDestoryChild();
			
			var txt : TextField;
			var listAry : Array;
			for each( var column : GridColumn in _columns )
			{
				if( _data != null && _data[ column.labelField ] != null )
				{
					if( _data[ column.labelField ] is String )
					{
						txt = addStringToTextField( _data[ column.labelField ] as String );
						addChild( txt );
						_list.push( txt );
					}
					else if( _data[ column.labelField ] is DisplayObject )
					{
						addChild( _data[ column.labelField ] as DisplayObject );
						_list.push( _data[ column.labelField ] );
					}
					else if( _data[ column.labelField ] is Array )
					{
						listAry = [];
						for each( var item : Object in _data[ column.labelField ] )
						{
							if( item is String )
							{
								txt = addStringToTextField( item as String );
								addChild( txt );
								listAry.push( txt );
							}
							else if( item is DisplayObject )
							{
								addChild( item as DisplayObject );
								listAry.push( item );
							}
						}
						
						_list.push( listAry );
					}
				}
				else
				{
					// 데이터가 null 일 경우.
					_list.push( null );
				}
			}
		}
		
		private function addStringToTextField( str:String ):TextField
		{
			var txt : TruncateTextField = new TruncateTextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
//			txt.mouseEnabled = false;
			txt.mouseWheelEnabled = false;
			txt.setText( str );
			txt.selectable = false;
			
			var tf : TextFormat = getDefaultTextFormat();
			txt.defaultTextFormat = tf;
			txt.setTextFormat( tf );
			
			return txt;
		}
		
		private function getDefaultTextFormat():TextFormat
		{
			var tf : TextFormat = new TextFormat("돋움", 12, 0x666666, false );
			return tf;
		}
		
		private function getMaskShape():Shape
		{
			var s : Shape = new Shape();
			s.graphics.beginFill(0x000000 );
			s.graphics.drawRect( 0,0,1,1);
			s.graphics.endFill();
			return s;
		}
		
		private function removeAndDestoryChild():void
		{
			var len:int = _list.length;
			for (var i:int = 0; i < len; i++) 
			{
				if( _list[ 0 ] != null )
				{
					var obj : Object = _list.shift();
					
					if( obj is DisplayObject )
					{
						if( this.contains( obj as DisplayObject ) )
						{
							removeChild( obj as DisplayObject );
							obj = null;
						}
					}
					else if( obj is Array )
					{
						for each( var disObj : DisplayObject in obj )
						{
							if( this.contains( disObj ) )
							{
								removeChild( disObj );
								disObj = null;
							}
						}
						
						obj = null;
					}
					
				}
				else
				{
					_list.shift();
				}
			}
		}
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
		override public function destory():void
		{
			super.destory();
		}
		
		
	}
}